#include "Triangle.h"

__INT32 Triangle::Init(char* strFileModel, char** strFileTextures, __UINT32 iTextures)
{
	Vertex verticesData[3];

	verticesData[0].m_v3Pos.x = 0.0f;  verticesData[0].m_v3Pos.y = 0.5f;  verticesData[0].m_v3Pos.z = 0.0f;
	verticesData[1].m_v3Pos.x = -0.5f;  verticesData[1].m_v3Pos.y = -0.5f;  verticesData[1].m_v3Pos.z = 0.0f;
	verticesData[2].m_v3Pos.x = 0.5f;  verticesData[2].m_v3Pos.y = -0.5f;  verticesData[2].m_v3Pos.z = 0.0f;

	//buffer Entity
	__UINT32 vboId;
	glGenBuffers(1, &vboId);
	glBindBuffer(GL_ARRAY_BUFFER, vboId);
	glBufferData(GL_ARRAY_BUFFER, sizeof(verticesData), verticesData, GL_STATIC_DRAW);
	glBindBuffer(GL_ARRAY_BUFFER, 0);

	m_Mesh = new Mesh();
	m_Mesh->Init(3, verticesData, 0, 0);
	ResourceManagement::GetInstance()->Add(RESOURCE_MESH, m_Mesh);
	m_Mesh = 0;
	m_Mesh = static_cast<Mesh*>(ResourceManagement::GetInstance()->Reference(RESOURCE_MESH, 0));

	return 0;
}

__VOID Triangle::Draw()
{
	glUseProgram(this->m_Shader->m_Program);

	if (m_Texture)
		m_Texture->Render(m_Shader);

	m_Mesh->Render(this->m_Shader);

	__INT32 size;
	glGetBufferParameteriv(GL_ARRAY_BUFFER, GL_BUFFER_SIZE, &size);
	size /= sizeof(Vertex);

	if (m_Mesh->GetIndexCount() != 0)
	{
		glDrawElements(GL_TRIANGLES, m_Mesh->GetIndexCount()*sizeof(GLuint), GL_UNSIGNED_INT, 0);
	}
	else
	{
		glDrawArrays(GL_TRIANGLES, 0, 3);
	}

	if (m_Texture)
		m_Texture->Unrender();
	m_Mesh->Unrender();
}